<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>试题列表展示</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/list.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>

</head>

<body>
    <div class="bookContainer">
        <h2>试题列表展示</h2>
        <div class="navbar-justify-between">
            <div>
                <button class="btn btn-outline-info" type="button" onclick="location.href='1.html'">添加试题</button>
                <button class="btn btn-outline-info" type="button" onclick="batchDelete()">批量删除</button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <td>选择</td>
                    <td class="width100">试题ID</td>
                    <td>题目</td>
                    <td>题目等级</td>
                    <td>正确答案</td>
                    <td>风险分值</td>
                    <td>风险标签</td>
                    <td>诈骗类型</td>
                    <td>题目类型</td>
                    <td>高风险选项</td>
                    <td id="optionA">选项A</td>
                    <td id="optionB">选项B</td>
                    <td id="optionC">选项C</td>
                    <td id="optionD">选项D</td>
                    <td class="width200">操作</td>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>

        <div class="demo">
            <ul id="pageContainer" class="pagination justify-content-center"></ul>
        </div>
        <script>

            getBookList();
            function getBookList() {
                $.ajax({
                    type : "get",
                    url : "api/questions/getListByPage"+ location.search, // location.search得到的是 ? 后面的查询字符串
                    success : function (result) {

                        // if (result == null || result.code == "UNLOGIN") {
                        //     alert("用户未登录,请先登录!");
                        //     location.href = "login.html";
                        // }
                        if (result == null) {
                            return;
                        }
                        var data = result;
                        var questions = data.records;
                        var tmp = "";
                        for (var question of questions) {
                            tmp  += '<tr>';
                            tmp  += '<td><input type="checkbox" name="selectQuestion" value="'+question.questionId+'" id="selectBook" class="selectQuestion"></td>';
                            tmp  += '<td>'+question.questionId+'</td>';
                            tmp  += '<td>'+question.content+'</td>';
                            tmp  += '<td>'+question.grade+'</td>';
                            tmp  += '<td>'+question.correctAnswer+'</td>';
                            tmp  += '<td>'+question.riskScore+'</td>';
                            tmp  += '<td>'+question.riskLabel+'</td>';
                            tmp  +='<td>'+question.cheatType+'</td>';
                            tmp  +='<td>'+(question.questionType == 'single'?'单选':'多选')+'</td>';
                            tmp  +='<td>'+question.highRiskOption+'</td>';
                            question.options.forEach(option => {
                                // 根据 key 找到对应的 <td> 并赋值
                                tmp  +='<td>'+option.optionContent+'</td>';
                                // $("#option" + option.optionKey).text(option.optionContent);
                            });
                            tmp  +='<td>';
                            tmp  +='<div class="op">';
                            tmp  +='<a href="book_update.html?questionId='+question.questionId+'">修改</a>';
                            tmp  +='<a href="javascript:void(0)" onclick="deleteQuestion('+question.questionId+')">删除</a>';
                            tmp  +='</div>';
                            tmp  +='</td>';
                            tmp  +='</tr>';
                        }
                        $("tbody").html(tmp);

                        //翻页信息
                        $("#pageContainer").jqPaginator({
                            totalCounts: data.total, //总记录数
                            pageSize: 10,    //每页的个数
                            visiblePages: 5, //可视页数
                            currentPage: data.pageRequest.currentPage,  //当前页码
                            first: '<li class="page-item"><a class="page-link">首页</a></li>',
                            prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页<\/a><\/li>',
                            next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页<\/a><\/li>',
                            last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页<\/a><\/li>',
                            page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}<\/a><\/li>',
                            //页面初始化和页码点击时都会执行
                            onPageChange: function (page, type) {
                                // console.log("第"+page+"页, 类型:"+type);
                                if (type == "change") {
                                    location.href = "book_list.html?currentPage=" + page;
                                }
                            }
                        });

                    },
                    error: function (error) {
                        alert("出现错误");
                        location.href = "book_list.html";
                    }
                });
            }
    

            function deleteQuestion(id) {
                var isDelete = confirm("确认删除?");
                if (isDelete) {
                    $.ajax({
                        type: "post",
                        url: "api/questions/deleteQuestion?questionId="+ id,
                        success: function (result) {
                            if (result) {
                                //删除图书
                                location.href = "book_list.html?currentPage=1"
                            }else {
                                alert("删除失败!");
                            }
                        },
                        error: function (error) {
                            alert("出现错误");
                            location.href = "book_list.html";
                        }
                    });

                }
            }
            function batchDelete() {
                var isDelete = confirm("确认批量删除?");
                if (isDelete) {
                    //获取复选框的id
                    var ids = [];
                    $("input:checkbox[name='selectQuestion']:checked").each(function () {
                        ids.push($(this).val());
                    });
                     console.log(ids);
                    $.ajax({
                        type: "post",
                        url: "api/questions/deleteBatch?ids="+ids ,
                        success: function (result) {
                            if (result) {
                                location.href = "book_list.html?currentPage=1";
                            }else {
                                alert("批量删除失败!");
                            }
                        },
                        error: function (error) {
                            alert("出现错误");
                            location.href = "book_list.html";
                        }
                    });
                }
            }

        </script>
    </div>
</body>

</html>